package com.iie.bigVideo.controller;


import com.iie.bigVideo.entity.Video;
import com.iie.bigVideo.entity.dto.VideoDto;
import com.iie.bigVideo.service.VideoService;
import com.iie.bigVideo.utils.PageUtils;
import com.iie.bigVideo.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XXX
 * @since 2023-11-24
 */
@RestController
@RequestMapping("/video")
@CrossOrigin //跨域
public class VideoController {

    @Autowired
    private VideoService videoService;


    /**
     * 查询 视频列表
     *
     * @param current    页码
     * @param size       每页多少
     * @param sort       排序  0降序 和 1升序 ；不传排序值默认是 0；  （required = false 必须对String类型 才起作用。）
     * @param directory  目录
     * @param volume     册
     * @param videoName  视频名
     * @param videoNotes 视频介绍
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @return
     */
    @GetMapping("/queryVideoList")
    public ResultVO queryVideoList(@RequestParam("current") int current, @RequestParam("size") int size,
                                   @RequestParam(value = "sort", required = false) String sort,
                                   @RequestParam(value = "directory", required = false) String directory,
                                   @RequestParam(value = "volume", required = false) String volume,
                                   @RequestParam(value = "videoName", required = false) String videoName,
                                   @RequestParam(value = "videoNotes", required = false) String videoNotes,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime) {
        HashMap<String, String> queryVideoParamHashMap = new HashMap<>();
        queryVideoParamHashMap.put("current", String.valueOf(current));
        queryVideoParamHashMap.put("size", String.valueOf(size));
        queryVideoParamHashMap.put("directory", directory);
        queryVideoParamHashMap.put("sort", sort);
        queryVideoParamHashMap.put("volume", volume);
        queryVideoParamHashMap.put("videoName", videoName);
        queryVideoParamHashMap.put("videoNotes", videoNotes);
        queryVideoParamHashMap.put("startTime", startTime);
        queryVideoParamHashMap.put("endTime", endTime);
        return videoService.queryVideoList(queryVideoParamHashMap);
    }

    /**
     * 添加视频
     *
     * @param videoName  视频名
     * @param videoNotes 视频介绍
     * @param catalogId  册ID
     * @param imgFile    图片
     * @param videoFile  视频
     * @return
     */
    @PostMapping("/addVideoObject")
    public ResultVO addVideoObject(@RequestParam(value = "videoName") String videoName, @RequestParam(value = "videoNotes", required = false) String videoNotes,
                                   @RequestParam(value = "catalogId") String catalogId, @RequestParam(value = "imgFile", required = false) MultipartFile imgFile,
                                   @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {
        Video video = new Video();
        video.setVideoName(videoName);
        video.setVideoNotes(videoNotes);
        video.setVideoIsDeleted(0);
        video.setCatalogId(Integer.parseInt(catalogId));
        return videoService.addVideoObject(video, videoFile, imgFile);
    }

    /**
     * 通过 视频 ID 查询
     *
     * @param videoId
     * @return
     */
    @GetMapping("/queryByVideoId/{videoId}")
    public ResultVO<VideoDto> queryByVideoId(@PathVariable("videoId") int videoId) {
        return videoService.queryByVideoId(videoId);
    }


    /**
     * @param videoId    视频ID
     * @param videoName  视频名称
     * @param videoNotes 视频介绍
     * @param catalogId  册ID
     * @param videoFile  视频
     * @param imgFile    图片
     * @return
     */
    @PostMapping("/updateVideo")
    public ResultVO updateVideo(@RequestParam(value = "videoId") String videoId,
                                @RequestParam(value = "videoName") String videoName,
                                @RequestParam(value = "videoNotes", required = false) String videoNotes,
                                @RequestParam(value = "catalogId") String catalogId,
                                @RequestParam(value = "videoFile", required = false) Object videoFile,
                                @RequestParam(value = "imgFile", required = false) Object imgFile) {
        Video video = new Video();
        video.setVideoId(Integer.parseInt(videoId));
        video.setVideoName(videoName);
        video.setVideoNotes(videoNotes);
        video.setCatalogId(Integer.parseInt(catalogId));
        if (imgFile instanceof MultipartFile && videoFile instanceof MultipartFile) {
            // 图片和视频是文件类型
            MultipartFile multipartFile = (MultipartFile) imgFile;
            MultipartFile videoFileObj = (MultipartFile) videoFile;
            return videoService.updateVideo(video, videoFileObj, multipartFile);
        } else if (imgFile instanceof MultipartFile) {
            // 图片是文件类型
            MultipartFile multipartFile = (MultipartFile) imgFile;
            return videoService.updateVideo(video, null, multipartFile);
        } else if (videoFile instanceof MultipartFile) {
            //视频是文件类型
            MultipartFile videoFileObj = (MultipartFile) videoFile;
            return videoService.updateVideo(video, videoFileObj, null);
        } else {
            //其他类型
            return videoService.updateVideo(video, null, null);
        }
    }

    /**
     * 删除视频
     *
     * @param videoId 视频ID
     * @return
     */
    @GetMapping("/deleteVideo/{videoId}")
    public ResultVO deleteVideo(@PathVariable("videoId") int videoId) {
        return videoService.deleteVideo(videoId);
    }

}

